<?php
header("Content-type:text/html; charset=utf-8"); 
date_default_timezone_set('PRC'); 
/**
 * http接口调用入口文件
 */
	// 当前目录
	if (!defined("ZJH_DIR")) {
		define("ZJH_DIR", dirname(__FILE__));	
	}

	// 包含头文件
	function path_format($file) {
		return ZJH_DIR . '/' . $file;
	}
	
	//自动加载
	include_once(path_format('lib/function.php'));
	set_include_path(get_include_path() . PATH_SEPARATOR . ZJH_DIR.'/lib/'. PATH_SEPARATOR .ZJH_DIR.'/config/');
	spl_autoload_register("autoload");
	/*
	include_once(path_format('lib/logger.php'));
	include_once(path_format('lib/apibase.php'));
	include_once(path_format('lib/function.php'));
	include_once(path_format('config/config.php'));
	include_once(path_format('config/game.php'));
	include_once(path_format('lib/mysql.php'));
	include_once(path_format('config/cmd.php'));
    include_once(path_format('lib/httpclient.php'));
    */
	
    $postData = file_get_contents('php://input');

    $data = json_decode($postData,true);
	if(empty($data)){
		echo json_encode(array('ret' => 1, "desc" => '请求参数有误.'));
	    exit(1); 
	}
	
    
	if(!empty($data)){
	    foreach($data as $key => $val){
		   if(is_array($val)){			   
			  $_GET[$key]=$val;			  
			}
			 $_GET[$key]=$val;
	    }
	}
	
	if(isset($_GET['param']) || !empty($_GET['param']))
	$_GET['param']=json_encode($_GET['param']);
	$class_name = null;
	if (isset($_GET['cmd'])) {	//目前没用cmd,全部改用action
		$class_name = Cmd::$cmd_array[$_GET['cmd']];
		echo json_encode(array('ret'=>100,"desc"=>"cmd = " . $_GET['cmd']));

	} elseif (isset($_GET['action'])) {
		$class_name = $_GET['action'];
//		file_put_contents('a.txt',$class_name."\n",FILE_APPEND);
	} else {
		echo json_encode(array('ret' => 2, "desc" => '请求参数有误.'));
		exit(1);

	}

	$file = path_format("postinterface/" . $class_name . ".php");

	if (isset($_GET['ver'])) {
		$file = path_format("postinterface/" . $_GET['ver'] . '/' .  $class_name . ".php");
	}
    
	if (file_exists($file)) {
		include_once($file);
		$cmd = new $class_name;
		$cmd->exec_logic();
	} else {
		echo json_encode(array('ret' => 3, "desc" => ''.$class_name.' no found.'));
		exit(2);
	}
	
?>
